package com.haotunet.app.goldtreasure.activity;

import android.content.ActivityNotFoundException;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.TitleView;

import java.io.File;

public class NewGuideLinesActivity extends BaseActivity implements OnClickListener {
    private TitleView mtTitleView;
    private ImageView ivCall;
    private RelativeLayout mQQLayout, mWXLayout, mWBLayout;
    private TextView mWXText, mWBText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_guidelines);
        initView();

    }

    private void initView() {
        mtTitleView = (TitleView) findViewById(R.id.title_view);
        mtTitleView.getTitletextView().setVisibility(View.VISIBLE);
        mtTitleView.getTitletextView().setText(getString(R.string.help_center));
        mtTitleView.getBackView().setVisibility(View.VISIBLE);
        mtTitleView.getBackView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ivCall = (ImageView) findViewById(R.id.iv_service_call);
        mQQLayout = (RelativeLayout) findViewById(R.id.rl_qq_layout);
        mWXLayout = (RelativeLayout) findViewById(R.id.rl_wx_layout);
        mWBLayout = (RelativeLayout) findViewById(R.id.rl_wb_layout);
        mWXText = (TextView) findViewById(R.id.text_wx);
        mWBText = (TextView) findViewById(R.id.text_wb);
        ivCall.setOnClickListener(this);
        mQQLayout.setOnClickListener(this);
        mWXLayout.setOnClickListener(this);
        mWBLayout.setOnClickListener(this);
    }

    @SuppressWarnings("deprecation")
    private void copyText(String text) {
        ClipboardManager cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        cmb.setText(text); // 将内容放入粘贴管理器,在别的地方长按选择"粘贴"即可
    }

    private void skipToOtherApp(String packgename) {
        final PackageManager pm = getPackageManager();
        Intent intent = pm.getLaunchIntentForPackage(packgename);
        if (null == intent) {// 没有获取到intent
            if (packgename.equals("com.sina.weibo")) {
                Uri uri = Uri.parse(
                        "https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F%3Fjumpfrom%3Dweibocom");
                try {
                    Intent intent1 = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent1);
                } catch (ActivityNotFoundException a) {
                    a.getMessage();
                }
            } else {
                UIHelper.showToast(NewGuideLinesActivity.this, "请安装微信客户端!");
            }
        } else {
            startActivity(intent);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_service_call:
                UIHelper.showCallRemindDialog(NewGuideLinesActivity.this, "400-181-9998");
                break;
            case R.id.rl_qq_layout:
                if (isInstallByread("com.tencent.mobileqq")) {
                    String url = "mqqwpa://im/chat?chat_type=crm&uin=938180156&version=1&src_type=web&web_src=http:://wpa.b.qq.com";
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                } else {
                    UIHelper.showToast(NewGuideLinesActivity.this, "请先安装QQ!");
                }
                break;
            case R.id.rl_wx_layout:
                copyText(mWXText.getText().toString());
                UIHelper.showToast(NewGuideLinesActivity.this, "已复制，打开微信搜索公众号");
                skipToOtherApp("com.tencent.mm");
                break;
            case R.id.rl_wb_layout:
                copyText(mWBText.getText().toString());
                UIHelper.showToast(NewGuideLinesActivity.this, "已复制");
                skipToOtherApp("com.sina.weibo");
                break;
            default:
                break;
        }
    }

    private boolean isInstallByread(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }
}
